package cn.kasa.org.service.impl;

import cn.kasa.basic.service.impl.BaseServiceImpl;
import cn.kasa.basic.utils.PageList;
import cn.kasa.org.domain.Department;
import cn.kasa.org.mapper.DepartmentMapper;
import cn.kasa.org.query.DepartmentQuery;
import cn.kasa.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
* @Description: service实现类
* @Author: LiuXinYu
* @Version: 1.0
* @Date:  2022/6/21 18:36
*/

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    /**
    * @Title: 新增
    * @Description: TODO
    * @Author: LiuXinYu
    * @Version: 1.0
    * @Date:  2022/6/21 18:37
    * @Parameters:
    * @Return
    */
    @Override
    @Transactional
    public void save(Department department) {
        departmentMapper.save(department);
        // 给dirPath拼接
        String dirPath = "";
        if (department.getParent() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parentDept  = departmentMapper.loadById(department.getParent().getId());
            if (parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    /**
    * @Title: 修改
    * @Description: TODO
    * @Author: LiuXinYu
    * @Version: 1.0
    * @Date:  2022/6/21 18:37
    * @Parameters:
    * @Return
    */
    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
        // 给dirPath拼接
        String dirPath = "";
        if (department.getParent() == null){
            dirPath = "/" + department.getId();
        }else {
            Department parentDept  = departmentMapper.loadById(department.getParent().getId());
            if (parentDept != null){
                dirPath = parentDept.getDirPath() + "/" + department.getId();
            }
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }






    @Override
    public List<Department> tree() {
        // 查出所有部门数据
        List<Department> depts = departmentMapper.loadAll();
        // 创建一个集合对象，放树数据
        List<Department> deptTree = new ArrayList<Department>();
        // 将集合数据转换为map，解决时间复杂度问题
       // Map<Long, Department> deptMap = depts.stream().collect(Collectors.toMap(Department::getId, item -> item));
        HashMap<Long, Department> deptMap = new HashMap<>();
        for (Department department : depts){
            deptMap.put(department.getId(), department);
        }
        // 从所有数据中获取一级数据
        for (Department deptTmp :depts){
            if (deptTmp.getParent_id() == null){
                deptTree.add(deptTmp);
            }else{
                Department department = deptMap.get(deptTmp.getParent_id());
                if (department != null){
                    department.getChildren().add(deptTmp);
                }
            }
        }

        return deptTree;
    }
}
